﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.DepartmentSelector;
using MS.InformationManagement.DAL.RecordCenter;
using System.Collections.Generic;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using System.Linq;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class DepartmentSelector : BaseControl, IDepartmentSelectorView
    {

        #region fields

        DepartmentSelectorPresenter _presenter;
        int? _selected;
        string _selectedDepartmentByName;

        #endregion

        #region Properties

        public bool ShowOnlyAllowedDependencies { get; set; }
        public IEnumerable<Seccion> Departments { get; set; }
        public bool ReadOnly { set { ddlDepartmentSelector.Enabled = !value; } }
        public int? SelectedDepartment { get { return int.Parse(ddlDepartmentSelector.SelectedValue); } set { _selected = value; } }
        public int? InitialDepartmentValue { get { return _selected.HasValue ? _selected.Value : new Nullable<int>(); } }
        public string SelectedDeparmentName { get { return ddlDepartmentSelector.SelectedItem.Text; } set { _selectedDepartmentByName = value; } }

        #endregion

        #region Form actions methods

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterAjaxLoaderPlugin();
            ddlDepartmentSelector.AddLoader();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new DepartmentSelectorPresenter(this);
                if (IsPostBack) return;
                _presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {

            ddlDepartmentSelector.DataSource = this.Departments.OrderBy(c => c.Title);
            ddlDepartmentSelector.DataBind();
            ddlDepartmentSelector.Items.Insert(0, new ListItem() { Text = GlobalConstants.DDL_INITIAL_STATE, Value = "0" });

            if (_selected.HasValue)
                ddlDepartmentSelector.SelectedIndex = ddlDepartmentSelector.Items.IndexOf((from item in ddlDepartmentSelector.Items.OfType<ListItem>()
                                                                                           where item.Value == _selected.ToString().Trim()
                                                                                           select item).FirstOrDefault());

            if (!string.IsNullOrEmpty(_selectedDepartmentByName))
                ddlDepartmentSelector.SelectedIndex = ddlDepartmentSelector.Items.IndexOf((from item in ddlDepartmentSelector.Items.OfType<ListItem>()
                                                                                           where item.Text == _selectedDepartmentByName.Trim()
                                                                                           select item).FirstOrDefault());

        }

        #endregion

        #region Public Methods

        public void SetRequired(string ValidationGroup)
        {
            ddlDepartmentSelector.Required(ExceptionConstants.ERROR_DDL_OBLIGATORIO, ValidationGroup, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
        }

        public void SetSelectionByName()
        {
            //ddlDepartmentSelector.SelectedIndex = ddlDepartmentSelector.Items.IndexOf((from item in ddlDepartmentSelector.Items.OfType<ListItem>()
            //                                                                           where item.Text == _selectedDepartmentByName.Trim()
            //                                                                           select item).FirstOrDefault());

            ddlDepartmentSelector.SelectedIndex = ddlDepartmentSelector.Items.IndexOf((from item in ddlDepartmentSelector.Items.OfType<ListItem>()
                                                                                       where item.Text.ToUpper().Replace(" ", string.Empty) == _selectedDepartmentByName.ToUpper().Replace(" ", string.Empty)
                                                                                       select item).FirstOrDefault());
        }

        #endregion

        public event DocDepartmentChangeHandler OnDocDepartamentChanged;

        public delegate void DocDepartmentChangeHandler(object sender, DocDepartamentChangeEventArgs e);

        protected void ddlDepartmentSelector_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (OnDocDepartamentChanged != default(DocDepartmentChangeHandler))
            {
                OnDocDepartamentChanged(this, new DocDepartamentChangeEventArgs { NewDependency = ddlDepartmentSelector.SelectedItem.Text, NewDepedencyID = ddlDepartmentSelector.SelectedItem.Value });
            }
        }


    }

    public class DocDepartamentChangeEventArgs : EventArgs
    {
        public string NewDependency { get; set; }
        public string NewDepedencyID { get; set; }
    }
}
